﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using V2TEK.NET.SH.Silverlight.Charts.Core.Schedule;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace V2TEK.NET.SH.Silverlight.Charts.GanttHelper
{
    public class SimpleTaskDescriptor : AbstractTaskDescriptor,  ITaskDescriptor,ITime1
    {
        public SimpleTaskDescriptor() : base()
        {
        }

        #region ITask Members

        public override string TaskName
        {
            get;
            set;
        }

        //public override float PercentComplete
        //{
        //    get
        //    {
        //        return 60;
        //    }
        //    set
        //    {

        //    }
        //}



        //public override IResource Resource
        //{
        //    get;
        //    set;
        //}


        #endregion

        #region ITime Members


        public DateTime StartDate
        {
            get { return (DateTime)GetValue(StartDateProperty); }
            set { SetValue(StartDateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StartDate.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StartDateProperty =
            DependencyProperty.Register("StartDate", typeof(DateTime), typeof(SimpleTaskDescriptor),
            new PropertyMetadata(default(DateTime), new PropertyChangedCallback(StartDateChanged)));

        protected static void StartDateChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (sender is SimpleTaskDescriptor)
            {
                ((SimpleTaskDescriptor)sender).StartDateChanged((DateTime)e.OldValue, (DateTime)e.NewValue);
            }
        }

        protected void StartDateChanged(DateTime oldValue, DateTime newValue)
        {
            if (!object.Equals(oldValue, newValue))
            {
                base.OnPropertyChanged(STARTDATE_PROPERTY);
                if (this.StartDate != DateTime.MinValue)
                    this.HasStartDate = true;
            }
        }

        public DateTime EndDate
        {
            get { return (DateTime)GetValue(EndDateProperty); }
            set { SetValue(EndDateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for EndDate.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EndDateProperty =
            DependencyProperty.Register("EndDate", typeof(DateTime), typeof(SimpleTaskDescriptor),
            new PropertyMetadata(default(DateTime), new PropertyChangedCallback(EndDateChanged)));

        protected static void EndDateChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (sender is SimpleTaskDescriptor)
            {
                ((SimpleTaskDescriptor)sender).EndDateChanged((DateTime)e.OldValue, (DateTime)e.NewValue);
            }
        }

        protected void EndDateChanged(DateTime oldValue, DateTime newValue)
        {
            if (!object.Equals(oldValue, newValue))
            {
                base.OnPropertyChanged(ENDDATE_PROPERTY);
                if (this.EndDate != DateTime.MinValue)
                    this.HasEndDate = true;
            }
        }



        
        //public override double TotalMilliseconds
        //{
        //    get
        //    {
        //        return
        //            this.Calendar.GetWorkTime(StartDate, EndDate).TotalMilliseconds;
        //    }
        //}

        #endregion

        #region ITaskDescriptor Members

        public override string DisplayText
        {
            get
            {
                return string.Format("开始时间\t{1}\r\n结束时间\t{2}\r\n完成百分比\t{3}", 
                    TaskName, 
                    StartDate.ToShortDateString(), 
                    EndDate.ToShortDateString(),
                    PercentComplete);
            }
        }

        public override string Description
        {
            get;
            set;
        }

        public override string Tooltip
        {
            get
            {
                return this.DisplayText;
            }
        }

        #endregion

        public override string ToString()
        {
            return this.WBS;
        }

        #region ICloneable<ITaskDescriptor> Members

        /// <summary>
        /// will be generated a new SimpleTaskDescriptor, but all members will not be initiazed
        /// </summary>
        /// <returns></returns>
        public override ITaskDescriptor Clone()
        {
            SimpleTaskDescriptor descriptor = new SimpleTaskDescriptor();
            return descriptor;
        }

        #endregion

        #region IHierarchicalData<ITaskDescriptor> Members


        public object DataSource
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public ObservableCollection<ITaskDescriptor> RecurToList()
        {
            throw new NotImplementedException();
        }

        #endregion




        public override double TotalMilliseconds
        {
            get { return (double)GetValue(TotalMillisecondsProperty); }
            set { SetValue(TotalMillisecondsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TotalMilliseconds.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TotalMillisecondsProperty =
            DependencyProperty.Register("TotalMilliseconds", typeof(double),
            typeof(SimpleTaskDescriptor), new PropertyMetadata(default(double), 
                new PropertyChangedCallback(TotalMillisecondsChanged)));

        protected static void TotalMillisecondsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (sender is SimpleTaskDescriptor)
            {
                ((SimpleTaskDescriptor)sender).TotalMillisecondsChanged((double)e.OldValue, (double)e.NewValue);
            }
        }

        protected void TotalMillisecondsChanged(double oldValue, double newValue)
        {
            if (!object.Equals(oldValue, newValue))
            {
                base.OnPropertyChanged(AbstractTaskDescriptor.TOTALMILLISECONDS_PROPERTY);
            }
        }

        private ICalendar mCalendar = null;
        public ICalendar Calendar
        {
            get
            {
                if (mCalendar == null)
                    mCalendar = new SimpleCalendar();
                return mCalendar;
            }
            set
            {
                mCalendar = value;
            }
        }
    }
}
